import publicForm from "@/components/public/el-form.vue"; //弹框form
import formJs from "./regexDetail.js"; //新增弹框配置项
import userImage from "@/components/public/userImage-upload.vue"; //弹框form

// 
import {
    savePersonalInfo, //保存
    getPersonalInfo, //详情

} from "@/api/BackStage/materialQualityPage/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow",

    ],
    data() {
        return {
            visible: "",
            loading: false,
            title: null,
            isLock: false,
            dialogHeight: null,
            treeData: [], //材料种类
            formData: {
                fId:"",//主键id，无:新增 有:编辑
		companyId: "",//标段id
		companyName: "",//标段名称
		name: "",//姓名
		gender: "",//性别
		certificateType: "",//证件类型
		certificateNum: "",//证件号码
		birthdays: "",//出生日期
		nation: "",//民族
		registered: "",//是否在母体注册(0:是,1:否)
		operatingPost: "",//工作岗位
		email: "",//电子邮件
		telephone: "",//手机号码
		provinceCity: "",//所在省市
		postalCode: "",//邮政编码
		address: "",//通信地址
		college: "",//毕业院校
		profession: "",//专业
		education: "",//学历
		graduationDates: "",//毕业时间
		workDates: "",//参加工作时间
		technicalTitles: "",//技术职称
		evaluationDates: "",//评定时间
		examiningDate: "",//从事试验检测年限
		officePhone: "",//办公室电话
		fax: "",//传真
		credentialCode: "",//试验检测证书编号
		credential: "",//试验检测证书
		workCondition: "",//工作情况
		picture: "",//图片
		recordfiles: []
            },
            formObj: formJs.formObj,
            formBcObj:formJs.formBcObj,
            btnShow: true,
            currentDetail: null,
            fileList: []
        };
    },
    components: {
        publicForm,
        userImage
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },

        statusNum: {
            handler(newVal) {
                if (newVal) {

                    this.edit(newVal);
                }
            },
            immediate: true,
        },
        currentRow: {
            handler(newVal) {
                if (newVal) {
                    this.currentDetail = newVal;
                    if (this.statusNum != 1) {
                        this.getDetail({
                            fId: this.currentDetail.fId
                        })
                    }
                }
            },
            immediate: true,
        },
    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：
    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
        this.btnShow = true

        if (this.statusNum == 1) { //新增
            this.formData = this.$options.data.call(this).formData;
            this.btnShow = true
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.formBcObj.map((item) => {
                item.disabled = false;
            });
           
            this.formData.picture = "picture" + Date.now();

            this.btnShow = true

        } else if (this.statusNum == 2) { //编辑
            this.formData.fId = this.currentDetail.fId //按钮 
            
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.formBcObj.map((item) => {
                item.disabled = false;
            });
            this.btnShow = true
        } else if (this.statusNum == 7) { //查看
            this.btnShow = false
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.formBcObj.map((item) => {
                item.disabled = true;
            });
        }
        this.getHeight();
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.76+ "px";
        },
        handleSelect(key) {
            this.activeIndex = key;
        },
        closeDialog() {
            this.$emit("close");
        },
        selectChange(data, a, b) {
            console.log(data, a, b);
        },
        edit() {
            this.formData = this.$options.data.call(this).formData;
        },
        upFileList(e){
            this.fileList=e;
            console.log(this.fileList);
        },
        // 刪除文件
        delteFile(file) {
            for (let index = 0; index < this.fileList.length; index++) {
                if (file.uid == this.fileList[index].uid) {
                    if (this.fileList[index].atta == undefined) {
                        this.formData.recordfiles.push({
                            fId: this.fileList[index].fId,
                        });
                    } else {
                        this.formData.recordfiles.push({
                            fId: this.fileList[index].atta.fId,
                        });
                    }
                    this.fileList.splice(index, 1);
                }
            }
        },
        // 详情
        getDetail(obj) {
            this.loading = true;
            getPersonalInfo(obj).then((res) => {
                if (res.errcode == 0) {
                    var data = res.data.yjQmpMsPersonalinfo
                    this.formData = data
                    this.loading = false;
                    if(this.formData.registered=='0'){
                        this.formData.registered="是";
                    }else {
                        this.formData.registered="否";
                    }
                    this.fileList=res.data.fFilelist;
                    // this.fileList=[{
                    //     url:"https://xayj.limsapp.cn:8383//fileAnnexes/20240714/dcdf91bd-424d-4de3-ae03-1204db3425c1/6b31c0aba8a0dc55e51c1a667fc5b2c.jpg"
                    // }]
                    console.log(4444,this.fileList);
                    // var selected = []
                    // if (data.fVarietyid != '') {
                    //     var id = data.fVarietyid.split("、")
                    //     var name = data.fVarietyname.split("、")
                    //     id.map((item, index) => {
                    //         selected.push({
                    //             label: name[index],
                    //             value: item
                    //         })
                    //     })
                    // } else {
                    //     selected = []
                    // }
                    // this.selected = [{
                    //     name: "fVarietyname1",
                    //     selected: selected
                    // }]
                    if (this.statusNum == 7) {
                        this.btnShow = false;
                    } else {
                        this.btnShow = true;
                    }
                } else {
                    this.loading = false;
                    this.$message.error(res.msg);
                }
            });
        },
        yj_saveForm(status) {
            this.formData.companyId=this.userInfo.companyId;//标段id
            this.formData.companyName=this.userInfo.companyFullName;//标段名称
            this.formData.recordfiles=[]
            console.log(777,this.fileList);
            if(this.fileList.length>1){
                this.fileList.map((item,index)=>{
                    if(index!=(this.fileList.length-1)){

                   
                    if (item.atta == undefined) {
                        this.formData.recordfiles.push({
                            fId: item.fId,
                        });
                    } else {
                        this.formData.recordfiles.push({
                            fId: item.atta.fId,
                        });
                    }
                }
                })

                
            }
            console.log(this.formData.recordfiles);
            // this.formData.userId = this.userInfo.userId; //当前登录人id
            // this.formData.userName = this.userInfo.realName; //当前登录人名称
            // var name = [];
            // var id = [];
            // this.formData.fVarietyname1.map(item => {
            //     name.push(item.label)
            //     id.push(item.value)
            // })
            // this.formData.fVarietyid = id.join("、")
            // this.formData.fVarietyname = name.join("、")
            // 0连续新增1新增
            if (this.$refs.publicForm.submitForm()) {
                this.saveForm(this.formData, status);
            } else {
                console.log("error");
            }
        },
        // 保存
        saveForm(data, status) {
            this.loading = true;
            if(this.formData.registered=='是'){
                this.formData.registered="0";
            }else {
                this.formData.registered="1";
            }
            // this.formData.typ = this.typs;
            this.isLock = true
            savePersonalInfo(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        this.visible = false;
                        this.$emit("refreshList", status);
                        this.$message.success("新增成功");
                        this.isLock = false
                    } else if (res.errcode == -1) {
                        this.loading = false;

                        this.visible = false;
                        this.$message.error(res.data);
                        this.$emit("refreshList", status);
                        this.isLock = false
                    } else {
                        this.loading = false;
                        this.isLock = false
                        this.$message.error(res.data);
                    }
                })
        },
    },
};